home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / apps / opaste / ditgray.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  2.8 KB  |  93 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.     ditgray - dither a grayscale image down to a few colors.
  19.     usage: ditgray <infile.bw> <outfile.ci>
  20.  
  21.     Tim Heidmann, Silicon Graphics
  22.     December 7, 1992
  23.     January 11, 1993   Add LEVEL5 and LEVEL8 ditherers (for rgb->ci tools)
  24.       March 31, 1993   getrow() workaround
  25. */
  26. #include <gl/image.h>
  27. #include <stdio.h>
  28. #include <math.h>
  29.  
  30. #ifdef SHADOW
  31. int  map[] = {0,255};
  32. int omap[] = {1,0};
  33. #else
  34. #ifdef LEVEL5
  35. int  map[] = {   0,  63, 127, 191, 255};
  36. int omap[] = {   0,   1,   2,   3,   4};
  37. #else
  38. #ifdef LEVEL8
  39. int  map[] = {   0,  37,  73, 110, 146, 183, 219, 255};
  40. int omap[] = {   0,   1,   2,   3,   4,   5,   6,   7};
  41. #else
  42. int  map[] = { -1,   0, 128, 255};
  43. int omap[] = {  0,   1,   2,   3};
  44. #endif
  45. #endif
  46. #endif
  47. int nMap = sizeof(map) / sizeof(int);
  48.  
  49. main(int argc, char *argv[]) {
  50.     short ibuf[4096], obuf[4096];
  51.     IMAGE *inf, *outf;
  52.     int irow, icol, iRand, iMap;
  53.  
  54.     if (argc != 3) {
  55.     fprintf(stderr, "usage: %s <infile.bw> <outfile.ci>\n", argv[0]);
  56.     exit(1);
  57.     }
  58.     if ((inf = iopen(argv[1], "r")) == NULL) {
  59.     fprintf(stderr, "Cannot open image file %s\n", argv[1]);
  60.     exit(1);
  61.     }
  62.     if (inf->zsize != 1)
  63.     fprintf(stderr, "Warning: %s has more than one channel.\n", argv[1]);
  64.  
  65. /* workaround 3/31/93 - Fix getrow() only returns 2bpp images correctly
  66.     outf = iopen(argv[2], "w", RLE(1), 3, inf->xsize, inf->ysize, 1);
  67. */
  68. outf = iopen(argv[2], "w", RLE(2), 3, inf->xsize, inf->ysize, 1);
  69.     if (outf == NULL) {
  70.     fprintf(stderr, "Cannot open image file %s\n", argv[1]);
  71.     exit(1);
  72.     }
  73.  
  74.     outf->colormap = CM_SCREEN;
  75.     for (irow = 0; irow < inf->ysize; irow++) {
  76.     getrow(inf, ibuf, irow, 0);
  77.     for (icol = 0; icol < inf->xsize; icol++) {
  78.         /* Find iMap s.t.  map[iMap - 1] < ibuf[icol] <= map[iMap]   */
  79.         for (iMap = 0; iMap < nMap && map[iMap] < ibuf[icol]; iMap++) ;
  80.         if (iMap == 0)         obuf[icol] = omap[0];
  81.         else if (iMap == nMap) obuf[icol] = omap[nMap - 1];
  82.         else {
  83.         iRand = random() % (map[iMap] - map[iMap-1]) + map[iMap-1] + 1;
  84.         obuf[icol] = omap[(ibuf[icol] >= iRand) ? iMap : (iMap - 1)];
  85.         }
  86.     }
  87.     putrow(outf, obuf, irow, 0);
  88.     }
  89.  
  90.     iclose(inf);
  91.     iclose(outf);
  92. }
  93.